class Solution(object):
    def intersect(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: List[int]
        """
        freq1 = {}
        freq2 = {}
        res = []
        for a in nums1:
            freq1[a] = freq1.get(a, 0) + 1
        for b in nums2:
            freq2[b] = freq2.get(b, 0) + 1
        for c in freq1:
            if c in freq2:
                res += [c] * min(freq1[c], freq2[c])
        return res
if __name__ == '__main__':
    nums1 = [1, 2, 2, 1]
    nums2 = [2, 2]
    print(Solution().intersect(nums1, nums2))